<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Closures  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
  </head>
  <body>
    <a title="Closures  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">Closures Docs</a></p>
        <p class="header-right"><a href="https://github.com/vhesener/Closures"><img src="img/gh.png"/>View on GitHub</a></p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">Closures Reference</a>
        <img id="carat" src="img/carat.png" />
        Closures  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Controls.html">Controls</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/UIButton.html">UIButton</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UISwitch.html">UISwitch</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UITextField.html">UITextField</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIDatePicker.html">UIDatePicker</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIPageControl.html">UIPageControl</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UISegmentedControl.html">UISegmentedControl</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIStepper.html">UIStepper</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UISlider.html">UISlider</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIControl.html">UIControl</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIBarButtonItem.html">UIBarButtonItem</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Scrolling Views.html">Scrolling Views</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/UITableView.html">UITableView</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UICollectionView.html">UICollectionView</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIPickerView.html">UIPickerView</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIScrollView.html">UIScrollView</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Gesture Recognizers.html">Gesture Recognizers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/UIGestureRecognizer.html">UIGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UITapGestureRecognizer.html">UITapGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UILongPressGestureRecognizer.html">UILongPressGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIPinchGestureRecognizer.html">UIPinchGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIPanGestureRecognizer.html">UIPanGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UISwipeGestureRecognizer.html">UISwipeGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIRotationGestureRecognizer.html">UIRotationGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIScreenEdgePanGestureRecognizer.html">UIScreenEdgePanGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIView.html">UIView</a>
              </li>
              <li class="nav-group-task">
                <a href="Gesture Recognizers.html#/s:8Closures9configure7gesture7handleryx_yxctSo19UIGestureRecognizerCRbzlF">configure(gesture:handler:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Controllers.html">Controllers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/UIImagePickerController.html">UIImagePickerController</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIImagePickerController/MediaFilter.html">– MediaFilter</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIImagePickerController/Result.html">– Result</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="KVO.html">KVO</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/_5FKeyValueCodingAndObserving.html">_KeyValueCodingAndObserving</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            
            <p><img src="https://raw.githubusercontent.com/vhesener/Closures/assets/assets/logo3.1.png" alt="Closures logo"></p>

<p><a href=""><img src="https://img.shields.io/badge/Swift-5.1-blue.svg?style=plastic&colorB=68B7EB" alt="Language"></a>
<a href=""><img src="https://img.shields.io/github/license/vhesener/Closures.svg?style=plastic&colorB=68B7EB" alt="License"></a>
<a href=""><img src="https://img.shields.io/github/release/vhesener/Closures.svg?style=plastic&colorB=68B7EB" alt="Release"></a></p>

<p><code>Closures</code> is an iOS Framework that adds <a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html">closure</a> handlers to many of the popular UIKit and Foundation classes. Although this framework is a substitute for some Cocoa Touch design patterns, such as <a href="https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html">Delegation &amp; Data Sources</a> and <a href="https://developer.apple.com/library/content/documentation/General/Conceptual/Devpedia-CocoaApp/TargetAction.html">Target-Action</a>, the authors make no claim regarding which is a <em>better</em> way to accomplish the same type of task. Most of the time it is a matter of style, preference, or convenience that will determine if any of these closure extensions are beneficial.</p>

<p>Whether you&rsquo;re a functional purist, dislike a particular API, or simply just want to organize your code a little bit, you might enjoy using this library.</p>

<hr>
<h2 id='a-href-usage-overview-usage-overview-a' class='heading'><a href="#usage-overview">Usage Overview</a></h2>
<h3 id='strong-convenient-closures-strong' class='heading'><strong>Convenient Closures</strong></h3>

<p>Some days, you just feel like dealing with <a href="https://vhesener.github.io/Closures/Controls.html">UIControl</a>&rsquo;s target-action using a closure instead.</p>
<pre class="highlight swift"><code><span class="n">button</span><span class="o">.</span><span class="n">onTap</span> <span class="p">{</span>
    <span class="c1">// UIButton tapped code</span>
<span class="p">}</span>
</code></pre>
<pre class="highlight swift"><code><span class="n">mySwitch</span><span class="o">.</span><span class="n">onChange</span> <span class="p">{</span> <span class="n">isOn</span> <span class="k">in</span>
    <span class="c1">// UISwitch value changed code</span>
<span class="p">}</span>
</code></pre>

<hr>

<p>Adding a <a href="https://vhesener.github.io/Closures/Gesture%20Recognizers.html">gesture recognizer</a> can be compacted into one method.</p>
<pre class="highlight swift"><code><span class="n">view</span><span class="o">.</span><span class="nf">addPanGesture</span><span class="p">()</span> <span class="p">{</span> <span class="n">pan</span> <span class="k">in</span>
    <span class="c1">// UIPanGesutreRecognizer recognized code</span>
<span class="p">}</span>
</code></pre>

<hr>

<p>Populating views with an array? I gotchu.</p>
<pre class="highlight swift"><code><span class="n">tableView</span><span class="o">.</span><span class="nf">addElements</span><span class="p">(</span><span class="n">myArray</span><span class="p">,</span> <span class="nv">cell</span><span class="p">:</span> <span class="kt">MyTableViewCell</span><span class="o">.</span><span class="k">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">element</span><span class="p">,</span> <span class="n">cell</span><span class="p">,</span> <span class="n">index</span> <span class="k">in</span>
    <span class="n">cell</span><span class="o">.</span><span class="n">textLabel</span><span class="o">!.</span><span class="n">text</span> <span class="o">=</span> <span class="s">"</span><span class="se">\(</span><span class="n">element</span><span class="se">)</span><span class="s">"</span>
<span class="p">}</span>
</code></pre>
<pre class="highlight swift"><code><span class="n">collectionView</span><span class="o">.</span><span class="nf">addFlowElements</span><span class="p">(</span><span class="n">myArray</span><span class="p">,</span> <span class="nv">cell</span><span class="p">:</span> <span class="kt">MyCustomCollectionViewCell</span><span class="o">.</span><span class="k">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">element</span><span class="p">,</span> <span class="n">cell</span><span class="p">,</span> <span class="n">index</span> <span class="k">in</span>
    <span class="n">cell</span><span class="o">.</span><span class="n">myImageViewProperty</span><span class="o">.</span><span class="n">image</span> <span class="o">=</span> <span class="n">element</span><span class="o">.</span><span class="n">thumbImage</span>
<span class="p">}</span>
</code></pre>
<pre class="highlight swift"><code><span class="n">pickerView</span><span class="o">.</span><span class="nf">addStrings</span><span class="p">(</span><span class="n">myStrings</span><span class="p">)</span> <span class="p">{</span> <span class="n">title</span><span class="p">,</span> <span class="n">component</span><span class="p">,</span> <span class="n">row</span> <span class="k">in</span>
    <span class="c1">// UIPickerView item selected code</span>
<span class="p">}</span>
</code></pre>

<hr>
<h3 id='strong-daisy-chaining-strong' class='heading'><strong>Daisy Chaining</strong></h3>

<p>Almost all convenience methods allow for the use of <a href="https://en.wikipedia.org/wiki/Method_chaining">daisy chaining</a>. This allows us to have some nice syntax sugar while implementing optional delegate methods in a concise way. Using <a href="https://vhesener.github.io/Closures/Extensions/UITextField.html">UITextField</a> as an example, we can organize and visualize all of the <code>UITextFieldDelegate</code> behavior.</p>
<pre class="highlight swift"><code><span class="n">textField</span>
    <span class="o">.</span><span class="n">didBeginEditing</span> <span class="p">{</span>
        <span class="c1">// UITextField did begin editing code</span>
    <span class="p">}</span><span class="o">.</span><span class="n">shouldClear</span> <span class="p">{</span>
        <span class="kc">true</span>
    <span class="p">}</span><span class="o">.</span><span class="n">shouldChangeCharacters</span> <span class="p">{</span> <span class="n">range</span><span class="p">,</span> <span class="n">string</span> <span class="k">in</span>
        <span class="c1">// some custom character change code</span>
        <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>
</code></pre>

<hr>
<h3 id='strong-retain-control-strong' class='heading'><strong>Retain Control</strong></h3>

<p>At no time are you locked into using these convenience methods. For instance, <a href="https://vhesener.github.io/Closures/Extensions/UITableView.html">UITableView</a> does not need to be populated with an array. You can just as easily provide your own <code>UITableViewDelegate</code> and <code>UITableViewDataSource</code> handlers.</p>
<pre class="highlight swift"><code><span class="n">tableView</span><span class="o">.</span><span class="nf">register</span><span class="p">(</span><span class="kt">MyTableViewCell</span><span class="o">.</span><span class="k">self</span><span class="p">,</span> <span class="nv">forCellReuseIdentifier</span><span class="p">:</span> <span class="s">"Cell"</span><span class="p">)</span>
<span class="n">tableView</span>
    <span class="o">.</span><span class="n">numberOfRows</span> <span class="p">{</span> <span class="n">_</span> <span class="k">in</span>
        <span class="n">myArray</span><span class="o">.</span><span class="n">count</span>
    <span class="p">}</span><span class="o">.</span><span class="n">cellForRow</span> <span class="p">{</span> <span class="n">indexPath</span> <span class="k">in</span>
        <span class="k">let</span> <span class="nv">cell</span> <span class="o">=</span> <span class="n">tableView</span><span class="o">.</span><span class="nf">dequeueReusableCell</span><span class="p">(</span><span class="nv">withIdentifier</span><span class="p">:</span> <span class="s">"Cell"</span><span class="p">,</span> <span class="nv">for</span><span class="p">:</span> <span class="n">indexPath</span><span class="p">)</span>
        <span class="n">cell</span><span class="o">.</span><span class="n">textLabel</span><span class="o">!.</span><span class="n">text</span> <span class="o">=</span> <span class="n">myArray</span><span class="p">[</span><span class="n">indexPath</span><span class="o">.</span><span class="n">row</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">cell</span>
    <span class="p">}</span><span class="o">.</span><span class="n">didSelectRowAt</span> <span class="p">{</span> <span class="n">indexPath</span> <span class="k">in</span>
        <span class="c1">// IndexPath selected code</span>
<span class="p">}</span>
</code></pre>

<hr>

<p>You aren&rsquo;t limited to which delegate/dataSource methods you wish to implement. Similarly, you can act on any
<a href="https://vhesener.github.io/Closures/Extensions/UIControl.html#/s:So9UIControlC8ClosuresE2onABXDSC0A6EventsV_yAB_So7UIEventCSgtc7handlertF">UIControl</a> events.</p>
<pre class="highlight swift"><code><span class="n">anyControl</span><span class="o">.</span><span class="nf">on</span><span class="p">(</span><span class="o">.</span><span class="n">touchDown</span><span class="p">)</span> <span class="p">{</span> <span class="n">control</span><span class="p">,</span> <span class="n">event</span> <span class="k">in</span>
    <span class="c1">// UIControlEvents.touchDown event code</span>
<span class="p">}</span>
</code></pre>

<hr>

<p>These two <a href="https://vhesener.github.io/Closures/Extensions/UIImagePickerController.html">UIImagePickerController</a> snippets are equivalent. As you can see, there are lots of ways to provide more granular control by mixing and match various convenience methods and closure handlers.</p>
<pre class="highlight swift"><code><span class="kt">UIImagePickerController</span><span class="p">(</span><span class="nv">source</span><span class="p">:</span> <span class="o">.</span><span class="n">camera</span><span class="p">,</span> <span class="nv">allow</span><span class="p">:</span> <span class="o">.</span><span class="n">image</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span><span class="p">,</span> <span class="n">picker</span> <span class="k">in</span>
    <span class="n">myImageView</span><span class="o">.</span><span class="n">image</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">editedImage</span>
<span class="p">}</span><span class="o">.</span><span class="nf">present</span><span class="p">(</span><span class="nv">from</span><span class="p">:</span> <span class="k">self</span><span class="p">)</span>
</code></pre>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">pickerController</span> <span class="o">=</span> <span class="kt">UIImagePickerController</span><span class="p">()</span>
<span class="n">pickerController</span><span class="o">.</span><span class="n">sourceType</span> <span class="o">=</span> <span class="o">.</span><span class="n">camera</span>
<span class="n">pickerController</span><span class="o">.</span><span class="n">mediaTypes</span> <span class="o">=</span> <span class="p">[</span><span class="n">kUTTypeImage</span><span class="p">]</span>
<span class="n">pickerController</span><span class="o">.</span><span class="n">didFinishPickingMedia</span> <span class="p">{</span> <span class="p">[</span><span class="k">weak</span> <span class="k">self</span><span class="p">]</span> <span class="n">info</span> <span class="k">in</span>
    <span class="n">myImageView</span><span class="o">.</span><span class="n">image</span> <span class="o">=</span> <span class="n">info</span><span class="p">[</span><span class="kt">UIImagePickerControllerEditedImage</span><span class="p">]</span> <span class="k">as?</span> <span class="kt">UIImage</span>
    <span class="k">self</span><span class="p">?</span><span class="o">.</span><span class="nf">dismiss</span><span class="p">(</span><span class="nv">animated</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
<span class="p">}</span><span class="o">.</span><span class="n">didCancel</span> <span class="p">{</span> <span class="p">[</span><span class="k">weak</span> <span class="k">self</span><span class="p">]</span> <span class="k">in</span>
    <span class="k">self</span><span class="p">?</span><span class="o">.</span><span class="nf">dismiss</span><span class="p">(</span><span class="nv">animated</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">self</span><span class="o">.</span><span class="nf">present</span><span class="p">(</span><span class="n">pickerController</span><span class="p">,</span> <span class="nv">animated</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
</code></pre>

<hr>
<h2 id='a-href-dive-deeper-dive-deeper-a' class='heading'><a href="#dive-deeper">Dive Deeper</a></h2>

<p>There are several ways to learn more about the <code>Closures</code> API, depending on your learning style. Some just like to open up Xcode and use autocomplete to view the various properties/functions. Others prefer a more documented approach. Below are some documentation options.</p>

<hr>
<h3 id='img-src-https-raw-githubusercontent-com-vhesener-closures-assets-assets-playground_icon-png-width-50-height-50-nbsp-nbsp-strong-playground-strong' class='heading'><img src="https://raw.githubusercontent.com/vhesener/Closures/assets/assets/playground_Icon.png" width="50" height="50"/> &nbsp;&nbsp; <strong>Playground</strong></h3>

<p>To play with the <a href="https://developer.apple.com/swift/blog/?id=35">Playground</a> demo, open the <code>Closures</code> workspace (Closures.xcworkspace file), build the <code>Closures</code> framework target, then click on the <code>ClosuresDemo</code> playground, and click on the play button:</p>

<p><img src="https://raw.githubusercontent.com/vhesener/Closures/assets/assets/playground_general2.gif" alt="Playgrounds"></p>

<hr>
<h3 id='img-src-https-raw-githubusercontent-com-vhesener-closures-assets-assets-reference_icon-png-width-50-height-50-nbsp-nbsp-strong-class-reference-documentation-strong' class='heading'><img src="https://raw.githubusercontent.com/vhesener/Closures/assets/assets/reference_Icon.png" width="50" height="50"/> &nbsp;&nbsp; <strong>Class Reference Documentation</strong></h3>

<p>The <a href="https://vhesener.github.io/Closures">Reference Documentation</a> has all of the detailed usage information including all the public methods, parameters, and convenience initializers.</p>

<p><a href="https://vhesener.github.io/Closures"><img src="https://raw.githubusercontent.com/vhesener/Closures/assets/assets/reference_large.png" alt="Class Reference Documentation"></a></p>

<hr>
<h2 id='a-href-installation-installation-a' class='heading'><a href="#installation">Installation</a></h2>
<h3 id='strong-swift-package-manager-strong' class='heading'><strong>Swift Package Manager</strong></h3>

<p>If using <a href="">Swift Package Manager</a>, in Xcode, go to <code>File &gt; Swift Packages &gt; Add Package Dependency...</code> and enter the following URL:</p>
<pre class="highlight plaintext"><code>https://github.com/vhesener/Closures
</code></pre>
<h3 id='strong-cocoapods-strong' class='heading'><strong>CocoaPods</strong></h3>

<p>If using <a href="https://cocoapods.org/">CocoaPods</a>, add the following to your Podfile:</p>
<pre class="highlight ruby"><code><span class="n">pod</span> <span class="s1">'Closures'</span>
</code></pre>
<h3 id='strong-carthage-strong' class='heading'><strong>Carthage</strong></h3>

<p>If using <a href="https://github.com/Carthage/Carthage">Carthage</a>, add the following to your Cartfile:</p>
<pre class="highlight shell"><code>github <span class="s2">"vhesener/Closures"</span>
</code></pre>
<h3 id='strong-manual-strong' class='heading'><strong>Manual</strong></h3>

<p>Download or clone the project files found in the <a href="https://github.com/vhesener/Closures">master branch</a>. Drag and drop
all .swift files located in the &lsquo;Closures/Source&rsquo; subdirectory into your Xcode project. Check the option <em>Copy items
if needed</em>. </p>

<hr>
<h2 id='a-href-background-background-a' class='heading'><a href="#background">Background</a></h2>

<p>Inspired by <a href="https://github.com/BlocksKit/BlocksKit">BlocksKit</a>, there was a need for a more <em>Swifty</em> version
of the same library. The goal of this library was to provide similar usefulness, but with the following
constraints:</p>

<ul>
<li>Use Swift&rsquo;s strong-typed system as much as possible in the API.</li>
<li>Not use the <a href="https://github.com/BlocksKit/BlocksKit/search?utf8=%E2%9C%93&q=objc_setAssociatedObject&type=">Objective-C runtime</a>. 
There are many reasons for this, but mostly because 

<ul>
<li>It was arbitrarily challenging.</li>
<li>It was in the spirit of Swift.</li>
</ul></li>
<li>Create a scalable mechanism to easily add additional closure wrappers in the future.</li>
</ul>

<p>It is our goal to become irrelevant via <a href="http://www.urbandictionary.com/define.php?term=sherlocked">sherlock</a>.
In addition to not having to support this library anymore, it would actually be flattering
to have been validated by the API folks at Apple.</p>

<hr>
<h2 id='a-href-want-more-want-more-a' class='heading'><a href="#want-more">Want more?</a></h2>

<p>If you were hoping to see an API converted using closures and came up empty handed, there&rsquo;s a
chance all can be right. <a href="https://github.com/vhesener/Closures/labels/Closure%20API%20Request">Simply vote on a feature</a> by adding a 👍 reaction.</p>

<hr>
<h2 id='a-href-license-license-a' class='heading'><a href="#license">License</a></h2>

<p>Closures is provided under the <a href="https://github.com/vhesener/Closures/blob/master/LICENSE">MIT License</a>.</p>
<pre class="highlight plaintext"><code>The MIT License (MIT)
Copyright (c) 2017 Vincent Hesener

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</code></pre>

          </section>
        </section>
        <section id="footer">
          <p>2017 Vincent Hesener</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.10.0</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
